package com.xadmin.sys.rbac.service.impl;

import com.xadmin.sys.common.utils.Page;
import com.xadmin.sys.rbac.mapper.UserMapper;
import com.xadmin.sys.rbac.pojo.Menu;
import com.xadmin.sys.rbac.pojo.Role;
import com.xadmin.sys.rbac.pojo.User;
import com.xadmin.sys.rbac.pojo.UserAndRole;
import com.xadmin.sys.rbac.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author vic Ly
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;

    /**
     * @param username
     * @return User
     * @Description 根据username获取个人信息
     */
    @Override
    public User getByName(String username) {
        return userMapper.getByName(username);
    }

    /**
     * @param userId
     * @return Menu
     * @Description 查询用户所有权限
     * @Param id
     */
    @Override
    public List<Menu> getMenuByUserId(Integer userId) {
        return userMapper.getMenuByUserId(userId);
    }

    /**
     * @param userId
     * @return Role
     * @Description 查询用户所有角色
     * @Param userId
     */
    @Override
    public List<Role> getRoleByUserId(Integer userId) {
        return userMapper.getRoleByUserId(userId);
    }

    /**
     * @param page
     * @return List<User>
     * @Description 分页查询
     * @Param page
     */
    @Override
    public Page<User> getAllByPage(Page<User> page) {
        List<User> userList = userMapper.getAllByPage(page);
        page.setList(userList);
        int tatalCount = userMapper.getCountByPage(page);
        page.setTotalCount(tatalCount);
        return page;
    }

    /**
     * @param idList
     * @return int
     * @Description 给用户批量添加角色
     * @Param idList
     */
    @Override
    public int addRoles(List<UserAndRole> idList) {
        int number = userMapper.addRoles(idList);
        return number;
    }

    /**
     * @param userId
     * @return int
     * @Description 批量删除用户角色
     * @Param userId
     */
    @Override
    public int deleteRoles(Integer userId) {
        int number = userMapper.deleteRoles(userId);
        return number;
    }

}
